﻿using System;
using System.ServiceProcess;
using mki.Backup.Helper;
using mki.Backup.Interfaces;

namespace mki.Backup.Service
{
	/// <summary>
	/// Represents the BackupService class
	/// </summary>
	public partial class BackupService : ServiceBase
	{
		#region Fields (1)

		private RunJobService runJobService;

		#endregion Fields

		#region Constructors (1)

		/// <summary>
		/// Initializes a new instance of the <see cref="BackupService"/> class.
		/// </summary>
		public BackupService()
		{
			InitializeComponent();
		}

		#endregion Constructors

		#region Properties (1)

		/// <summary>
		/// Gets a value indicating whether [services started].
		/// </summary>
		/// <value>
		///   <c>true</c> if [services started]; otherwise, <c>false</c>.
		/// </value>
		public Boolean ServicesStarted
		{
			get
			{
				return this.runJobService != null;
			}
		}

		#endregion Properties

		#region Methods (4)

		// Public Methods (2) 

		/// <summary>
		/// Starts the services.
		/// </summary>
		public void StartServices()
		{
			try
			{
				Logging.WriteVerbose("Starting Backup Service...");
				this.runJobService = new RunJobService();
				Logging.WriteVerbose("Backup Service started...");
			}
			catch (Exception ex)
			{
				Logging.WriteError("Backup service start error : {0}", ex.Message);
			}
		}

		/// <summary>
		/// Stops the services.
		/// </summary>
		public void StopServices()
		{
			try
			{
				if (runJobService != null)
				{
					Logging.WriteVerbose("Attempting to stop Backup serivce...");
					this.runJobService.Stop();
					Logging.WriteVerbose("Backup service stopped.");
				}
			}
			catch (Exception ex)
			{
				Logging.WriteError("Backup service stop error : {0}", ex.Message);
			}
		}
		// Protected Methods (2) 

		/// <summary>
		/// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
		/// </summary>
		/// <param name="args">Data passed by the start command.</param>
		protected override void OnStart(string[] args)
		{
			this.StartServices();
		}

		/// <summary>
		/// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
		/// </summary>
		protected override void OnStop()
		{
			this.StopServices();
		}

		#endregion Methods
	}
}